﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace FinalPointSet.Model.Util
{
    public static class MethodUtil
    {
        /// <summary>
        /// 返回长字符串是否包含短字符串
        /// </summary>
        /// <param name="str1">字符串1</param>
        /// <param name="str2">字符串2</param>
        /// <returns>是否包含</returns>
        public static bool IsMatch(string str1, string str2)
        {
            string lowStr1 = str1.ToLower();
            string lowStr2 = str2.ToLower();
            if (lowStr1.Length > lowStr2.Length)
            {
                return lowStr1.Contains(lowStr2);
            }
            else
            {
                return lowStr2.Contains(lowStr1);
            }
        }

        /// <summary>
        /// 返回两个字符串之间的相似度
        /// </summary>
        /// <param name="proName1">字符串1</param>
        /// <param name="proName2">字符串2</param>
        /// <returns>相似度</returns>
        public static double GetSimilarityRatio(string proName1, string proName2)
        {
            int Length1 = proName1.Length;
            int Length2 = proName2.Length;

            int Distance = 0;
            if (Length1 == 0)
            {
                Distance = Length2;
            }
            if (Length2 == 0)
            {
                Distance = Length1;
            }
            if (Length1 != 0 && Length2 != 0)
            {
                int[,] Distance_Matrix = new int[Length1 + 1, Length2 + 1];
                //编号
                int Bianhao = 0;
                for (int i = 0; i <= Length1; i++)
                {
                    Distance_Matrix[i, 0] = Bianhao;
                    Bianhao++;
                }
                Bianhao = 0;
                for (int i = 0; i <= Length2; i++)
                {
                    Distance_Matrix[0, i] = Bianhao;
                    Bianhao++;
                }
                char[] Str_1_CharArray = proName1.ToCharArray();
                char[] Str_2_CharArray = proName2.ToCharArray();
                for (int i = 1; i <= Length1; i++)
                {
                    for (int j = 1; j <= Length2; j++)
                    {
                        if (Str_1_CharArray[i - 1] == Str_2_CharArray[j - 1])
                        {
                            Distance = 0;
                        }
                        else
                        {
                            Distance = 1;
                        }
                        int Temp1 = Distance_Matrix[i - 1, j] + 1;
                        int Temp2 = Distance_Matrix[i, j - 1] + 1;
                        int Temp3 = Distance_Matrix[i - 1, j - 1] + Distance;
                        Distance_Matrix[i, j] = Math.Min(Temp1, Temp2);
                        Distance_Matrix[i, j] = Math.Min(Distance_Matrix[i, j], Temp3);
                    }
                }
                Distance = Distance_Matrix[Length1, Length2];
            }
            double Aerfas = 1 - 1.0 * Distance / (Math.Max(Length1, Length2));
            return Aerfas;
        }
        public static void OpenFileLocationInExplorer(string filePath)
        {
            try
            {
                // 如果输出目录不存在，则创建它  
                if (!Directory.Exists(filePath))
                {
                    MessageBox.Show($"文件路径不存在\n{filePath}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                // 启动资源管理器并导航到该目录  
                Process.Start(new ProcessStartInfo
                {
                    FileName = "explorer.exe",
                    Arguments = $"\"{filePath}\"",
                    UseShellExecute = true
                });
            }
            catch (Exception ex)
            {
                // 处理异常（例如，如果资源管理器无法启动）  
                MessageBox.Show($"无法打开文件位置: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        public static System.Drawing.Point PointFormat(Point point)
        {
            return new System.Drawing.Point((int)point.X, (int)point.Y);
        }

        public static Point PointFormat(System.Drawing.Point point)
        {
            return new Point(point.X, point.Y);
        }

        /// <summary>
        /// 转置集合
        /// </summary>
        /// <param name="matrix"></param>
        /// <returns></returns>
        public static List<List<string>> Transpose(List<List<string>> matrix)
        {
            if (matrix == null || matrix.Count == 0 || matrix.All(row => row.Count == 0))
                return [];

            int rowCount = matrix.Count;
            int colCount = matrix.Max(row => row.Count);

            List<List<string>> transposed = new(colCount);
            for (int i = 0; i < colCount; i++)
            {
                transposed.Add(new List<string>(rowCount));
                for (int j = 0; j < rowCount; j++)
                {
                    if (i < matrix[j].Count)
                    {
                        transposed[i].Add(matrix[j][i]);
                    }
                    else
                    {
                        transposed[i].Add(""); // 或者使用空字符串 ""
                    }
                }
            }

            return transposed;
        }
    }
}
